import http from '@/utils/http';
import type { 
  ChatRecord, 
  ChatQueryParams, 
  ChatStatistics,
  ChatApiResponse,
  BatchDeleteRequest
} from '@/types';

/**
 * 获取普通对话记录列表（支持搜索和分页）
 */
export const getChatList = async (params: ChatQueryParams): Promise<ChatApiResponse<ChatRecord>> => {
  const response = await http.get('/admin/chat', { params });
  return response.data;
};

/**
 * 获取普通对话记录详情
 */
export const getChatDetail = async (id: number): Promise<ChatRecord> => {
  const response = await http.get(`/admin/chat/${id}`);
  return response.data.data;
};

/**
 * 删除单条普通对话记录
 */
export const deleteChatRecord = async (id: number): Promise<void> => {
  await http.delete(`/admin/chat/${id}`);
};

/**
 * 批量删除普通对话记录
 */
export const batchDeleteChatRecords = async (ids: number[]): Promise<{
  deletedCount: number;
}> => {
  const response = await http.delete('/admin/chat/batch', { data: { ids } });
  return response.data.data;
};

/**
 * 获取某个房间的所有聊天记录
 */
export const getRoomChatRecords = async (roomId: string, sort?: string): Promise<ChatRecord[]> => {
  const response = await http.get(`/admin/chat/room/${roomId}`, {
    params: { sort }
  });
  return response.data.data;
};

/**
 * 删除某个房间的所有聊天记录
 */
export const deleteRoomChatRecords = async (roomId: string): Promise<{
  deletedCount: number;
}> => {
  const response = await http.delete(`/admin/chat/room/${roomId}`);
  return response.data.data;
};

/**
 * 删除某个用户的所有对话记录
 */
export const deleteUserChatRecords = async (userId: number): Promise<{
  deletedCount: number;
}> => {
  const response = await http.delete(`/admin/chat/user/${userId}`);
  return response.data.data;
};

/**
 * 清理过期的聊天记录
 */
export const cleanupExpiredChatRecords = async (days: number = 30): Promise<{
  deletedCount: number;
  cleanupDays: number;
}> => {
  const response = await http.delete('/admin/chat/cleanup/expired', {
    params: { days }
  });
  return response.data.data;
};

/**
 * 标记消息为已读/未读
 */
export const updateChatReadStatus = async (id: number, isRead: boolean): Promise<void> => {
  await http.put(`/admin/chat/${id}/read-status`, { is_read: isRead });
};

/**
 * 批量标记房间消息为已读
 */
export const markRoomMessagesAsRead = async (roomId: string): Promise<{
  updatedCount: number;
}> => {
  const response = await http.put(`/admin/chat/room/${roomId}/mark-read`);
  return response.data.data;
};

/**
 * 获取普通对话统计信息
 */
export const getChatStatistics = async (): Promise<ChatStatistics> => {
  const response = await http.get('/admin/chat/stats/overview');
  return response.data.data;
}; 